package com.steakliu.sun.registry.transfer;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.steakliu.sun.common.meta.ServiceMeta;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 功能说明： 各种注册中心的服务元数据转为Rpc的元数据ServiceMeta实体
 * <p>
 * Original @Author: steakliu-刘牌, 2022-12-26  21:48
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
public class ServiceMetaTransfer {
  private static volatile List<ServiceMeta> nacosCacheServiceMetaList = new CopyOnWriteArrayList<>();


  public static List<ServiceMeta> getNacosCacheServiceMetaList(List<Instance> list){
    if (list.isEmpty() || list.size() == nacosCacheServiceMetaList.size()){
      return nacosCacheServiceMetaList;
    }
    nacosCacheServiceMetaList.clear();
    list.forEach(instance -> {
      nacosCacheServiceMetaList.add(JSON.parseObject(instance.getMetadata().get("meta"),ServiceMeta.class));
    });
    return nacosCacheServiceMetaList;
  }
}
